<template>
    <div>
        <el-table :data="goodsList" border style="width: 100%">
            <el-table-column prop="id" label="商品编号" width="180">
            </el-table-column>
            <el-table-column prop="goodsname" label="商品名称" width="180">
            </el-table-column>
            <el-table-column prop="price" label="商品价格">
            </el-table-column>
            <el-table-column prop="market_price" label="市场价格" width="180">
            </el-table-column>
            <el-table-column prop="img" label="图片" width="180">
                <template slot-scope="item">
                    <div>
                        <img v-if="item.row.img" :src="item.row.img" class="img" alt="" srcset="">
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="isnew" label="是否新品">
            </el-table-column>
            <el-table-column prop="ishot" label="是否热卖" width="180">
            </el-table-column>
            <el-table-column prop="status" label="状态" width="180">
            </el-table-column>
            <el-table-column prop="address" label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" @current-change="changeNum" :page-size="size" :total="count">
        </el-pagination>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import { goodsDelete } from '../../request/api';
export default {
    data() {
        return {}
    },
    methods: {
        ...mapActions({
            goods_list: 'goods/goodsListAction',
            goods_count: 'goods/goodsCountActions',
            goods_page: 'goods/changePage'
        }),
        // 更改页码
        changeNum(num) {
            // console.log(num);
            this.goods_page(num)
        },
        // 删除一条数据
        del(id) {
            // 询问用户是否要删除
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //调取删除接口
                goodsDelete({ id }).then(res => {
                    if (res.code == 200) {
                        // 删除成功
                        this.$message.success(res.msg);
                        // 重新渲染视图
                        this.goods_list();
                        // 重新获取总条数
                        this.goods_count();
                    } else {
                        // 删除失败
                        this.$success.error(res.msg);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 编辑
        edit(id) {
            this.$emit('edit', id);
        }
    },
    mounted() {
        this.goods_list();
        this.goods_count();
    },
    watch: {},
    computed: {
        ...mapGetters({
            goodsList: 'goods/getGoodsList',
            size: 'goods/getSize',
            count: 'goods/getCount'
        })
    },
    filters: {},
    components: {

    },
}
</script>
<style scoped>
.el-pagination {
    float: right;
}

.img {
    width: 80px;
    height: 80px;
}
</style>